package com.spit.fammo.adapter.bindadapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import androidx.appcompat.widget.AppCompatTextView
import com.spit.fammo.R
import com.spit.fammo.bean.DataBean


class UploadAdapter(
    private val context: Context,
    private val listBean: List<DataBean>
) : BaseExpandableListAdapter() {

    override fun getGroupCount(): Int = listBean.size

    override fun getChildrenCount(groupPosition: Int): Int {
        return listBean[groupPosition].expandableChild.size
    }

    override fun getGroup(groupPosition: Int): Any {
        return listBean[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return listBean[groupPosition].expandableChild?.get(childPosition) ?: 0
    }

    override fun getGroupId(groupPosition: Int): Long = groupPosition.toLong()

    override fun getChildId(groupPosition: Int, childPosition: Int): Long = childPosition.toLong()

    override fun hasStableIds(): Boolean = false

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup
    ): View {
        val view: View = convertView ?: LayoutInflater.from(context).inflate(R.layout.i_upload_group, parent, false)
        val bean = listBean[groupPosition]
        val tv_title: AppCompatTextView = view.findViewById(R.id.tv_title)
        val tv_status: AppCompatTextView = view.findViewById(R.id.tv_status)
        tv_title.text = bean.name
        tv_status.setText(if (isExpanded) "-" else "+")
        /*view.setBackgroundColor(
            if (isExpanded) {
                context.getColor(R.color.colorPrimaryDark)
            } else {
                Color.TRANSPARENT // 未选中时的背景色
            }
        )*/
        return view
    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup
    ): View {
        val parentBean = listBean[groupPosition]
        val bean = parentBean.expandableChild.get(childPosition)
        val view: View = convertView ?: LayoutInflater.from(context).inflate(R.layout.i_upload_child, parent, false)
        val tv_title: AppCompatTextView = view.findViewById(R.id.tv_title)
        tv_title.text = bean.orderId + " | " + bean?.name

        // 设置 Child 的背景颜色
        /*view.setBackgroundColor(
            if (bean.isSelect) {
                context.getColor(R.color.colorPrimaryDark)
            } else {
                Color.TRANSPARENT // 未选中时的背景色
            }
        )*/

        return view
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = true

}